using System;
using System.Collections.Generic;
using System.Text;

namespace Test {
	class IronMan {

		static int ParseTS(string tspan) {
			int ret=-1;
			TimeSpan ts=default(TimeSpan);
			if(TimeSpan.TryParse(tspan,out ts))
				ret=(int)ts.TotalSeconds;
			return ret;
		}

		static void MainX() {
			MAL mal=new MAL();
			mal.NonQry("truncate table iron");
			//mal.NonQry("create table iron(bib int primary key,name varchar(100),ctr varchar(3),divi int,gend int,ove int,swim int,bike int,run int,fin int,points int)");
			ZCache pld=new ZCache("iron");


			for(int i=1;i<74;i++)
			{
				string link=string.Format("http://www.ironman.com/triathlon/events/emea/ironman-70.3/italy/results.aspx?p={0}&race=italy70.3&rd=20140601",i);
				string page=pld.Get(link,5500);

				string tab=Nod.Dbl(page,"<table id=\"eventResults\"");
				foreach(string row in Nod.Dbls(tab,"<tr>"))
				{
					string rowf=row.Replace("</a></span>","</a></td>");
					string[] cells=Nod.Dbls(rowf,"<td>");
					if(cells.Length==10)
					{
						string bib=Nod.Rx(cells[0],";bidid=(\\d+?)&");
						string name=Nod.InnerText(cells[0]);
						string cntr=Nod.InnerText(cells[1]);
						string divi=Nod.InnerText(cells[2]);
						if(divi=="---")
							divi="-1";

						string gend=Nod.InnerText(cells[3]);
						if(gend=="---")
							gend="-1";

						string ove=Nod.InnerText(cells[4]);
						if(ove=="---")
							ove="-1";

						string swim=Nod.InnerText(cells[5]);
						int swimi=ParseTS(swim);
						string bike=Nod.InnerText(cells[6]);
						int bikei=ParseTS(bike);

						string run=Nod.InnerText(cells[7]);
						int runi=ParseTS(run);

						string fin=Nod.InnerText(cells[8]);
						int fini=ParseTS(fin);

						string points=Nod.InnerText(cells[9]);
						if(points=="---")
							points="-1";

						mal.NonQry("insert into iron(bib,name,ctr,divi,gend,ove,swim,bike,run,fin,points)values({0},'{1}','{2}',{3},{4},{5},{6},{7},{8},{9},{10})",bib,name.Replace("'",""),cntr,divi,gend,ove,
							swimi,bikei,runi,fini,points);
					}

				}


			}


		}
	}
}
